/*
 *
 * Copyright(c) TODO
 * licensing@realtor.org
 * http://www.realtor.org/license
 */

Ext.onReady(function() {

	var rec_user_saved_items = Ext.data.Record.create( [ {name : 'rec_id'},{name : 'created_date'},{name : 'raw_frm_vars'},{name : 'comps'}	]);
	var rdr_user_saved_items = new Ext.data.XmlReader( {record : "row", id : "rec_id"}, rec_user_saved_items);

	var url_load = AD.Globals.URL_user_data + "load";
	var proxy_user_saved_items = new Ext.data.HttpProxy( { url : url_load});
	var ds_user_saved_items = new Ext.data.Store( {proxy : proxy_user_saved_items,	reader : rdr_user_saved_items});

	function user_data_reset()
	{
		AD.UI.user_data_win = null;
		AD.UI.user_data_grid = null;
		AD.UI.comps_to_load = "";
	}
	user_data_reset();
	var get_selected_item = function()
	{
		var grid =  AD.UI.user_data_grid;				
        var sel = grid.getSelectionModel().getSelections();

		if(sel.length < AD.Globals.MAX_PROPS)
		{
			return sel[0].data;
		}
		return null;
	};

	function load_field(field_name_value)
	{
		var nv_fv = field_name_value.split("=");
		
		var cmp = Ext.getCmp(nv_fv[0]);
		if(!cmp)
		{
			alert(nv_fv[0]);
			return;
		}else
		{
			val = cmp.setValue(nv_fv[1]);
		}

	}
	
	var load_session = function()
	{
		var rec = get_selected_item();
		
		AD.UI.user_data_win.hide();
		
		var fv = rec.raw_frm_vars;
		AD.UI.frm_search.form.reset();
		var ar_fv = fv.split(",");
		for(i=0; i< ar_fv.length; i++)
		{
			load_field(ar_fv[i]);
		}
		
		AD.UI.fn_search();
		AD.UI.comps_to_load = rec.comps;
		
		//selectRecords
		//Select Comps
		//var load_comps = 
	};



		var fn_user_data_load = function() {
			
			//load_mask.hide();
			
			if(null == AD.UI.user_data_win)
			{
				AD.UI.user_data_win = new Ext.Window(
					{
						id : 'user_data_win',
						width : AD.Globals.client_width * 0.30,
						height : AD.Globals.client_height * 0.60,
						autoScroll : true,
						collapsible : true,
						collapsed : false,
						closeAction : 'close',
						closable : true,
						frame : true,
						layout : 'fit',
						minimizable : false,
						maximizable : true,
						stateful : true,
						stateId : 'user_data_win',
						title : '',
						items : [ {
							xtype : 'box',
							autoEl : {
								tag : 'div',
								html : "<div id='saved_sessions_grid' 	style='width: 100%; height:  90%;'></div>"
							}
							}]
					});
			}
			AD.UI.user_data_win.show(AD.UI.frm_search);
			
			if(null == AD.UI.user_data_grid)
			{
				var sm_loader = new Ext.grid.CheckboxSelectionModel({singleSelect : true, header : ''});
				
				AD.UI.user_data_grid = new Ext.grid.GridPanel( {
					id : "sessions_grid",
					title : 'Saved Sessions',
					store : ds_user_saved_items,
	
					autoScroll : true,
					collapsible : false,
					columnLines : true,
					frame : false,
					stateful : true,
					viewConfig : {
						forceFit : true
					},
					width : '100%',
					height : AD.Globals.form_height,
					tbar: [{text:"Load", handler : load_session }],

					cm : new Ext.grid.ColumnModel( {
						defaults : {
							sortable : true,
						},
						columns : [ sm_loader,
						{
							header : "Saved"
							,dataIndex : 'created_date'
							,width : 150	
						}, {
							header : "Parameters"
							,dataIndex : 'raw_frm_vars'
								,width : 300
						}, {
							header : "Comparables"
							,dataIndex : 'comps'
							,width : 100
						}
	
						]
					}),
					sm : sm_loader,
					renderTo : 'saved_sessions_grid'
				});
				AD.UI.user_data_grid.on('rowdblclick', load_session, AD.UI.user_data_grid)
			}
		};

		AD.UI.user_data_file_open = function() {
			
			user_data_reset();
			
			
			ds_user_saved_items.on('load', function() { fn_user_data_load(); });

			//load_mask = new Ext.LoadMask(Ext.getBody(), { msg : 'Loading, please wait...' }, { removeMask : true}, { store : ds_user_saved_items});
			ds_user_saved_items.reload();
			//load_mask.show();

		};
	});
